package de.baumann.browser.activitys.oc

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.activitys.BindPoolActivity
import de.baumann.browser.activitys.hash.BuyHashActivity
import de.baumann.browser.adapter.PagerAdapter
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.fragments.OcAllFragment
import de.baumann.browser.fragments.OcFailedFragment
import de.baumann.browser.fragments.OcInFragment
import de.baumann.browser.fragments.OcOutFragment
import de.baumann.browser.iview.IBaseView
import de.baumann.browser.utils.getInviteCodeStatus
import de.baumann.browser.utils.web3j.BalanceTask2
import de.baumann.browser.views.dialog.InviteCodeDialog
import kotlinx.android.synthetic.main.activity_oc_b_detail.*
import kotlinx.android.synthetic.main.include_tool_bar.*

class OCBDetailActivity : BaseOdinActivity<IBaseView, BasePresenter<IBaseView>>() {

    companion object {
        fun startOCBDetailActivity(context: Context, n: String, a: String, cn: String, url: String, ca: String) {
            context.startActivity(Intent(context, OCBDetailActivity::class.java).apply {
                putExtra("n", n)
                putExtra("a", a)
                putExtra("cn", cn)
                putExtra("url", url)
                putExtra("ca", ca)
            })
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_oc_b_detail
    }

    override fun getTitleText(): String {
        return "ODIN 钱包"
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        cqbmc.text = intent.getStringExtra("n")
        cn.text = getCn()

        csk.setOnClickListener {
            CollectMoneyActivity.startCollectMoneyActivity(mContext, getA(), getCn())
        }

        czz.setOnClickListener {
            OCBTransferActivity.startOCBTransferActivity(mContext, getA(), getUrl(), getCA(), getCn())
        }

        val all = OcAllFragment.instance
        val out = OcOutFragment.instance
        val intr = OcInFragment.instance
        val fail = OcFailedFragment.instance
        val bundle = Bundle().apply {
            putString("address", getA())
            putString("cn", getCn())
        }
        all.arguments = bundle
        out.arguments = bundle
        intr.arguments = bundle
        fail.arguments = bundle

        val fragmentList = arrayListOf(all, out, intr, fail)

        val titles = arrayOf("全部", "转出", "转入", "失败")
        val adapter = PagerAdapter(supportFragmentManager, fragmentList, titles)
        vc.adapter = adapter
        tlc.setupWithViewPager(vc)
    }

    private fun getCn(): String {
        return intent.getStringExtra("cn")
    }

    var x = ""
    override fun initData() {

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        when {
            getCn() == "gung" -> {
                menuInflater.inflate(R.menu.menu_exchange, menu)
            }
            getCn() == "frig" -> {
                menuInflater.inflate(R.menu.menu_exchange, menu)
            }
            else -> {
            }
        }

        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        x = cBalance.text.toString()
        if (x.toDouble() > 0) {
            if (getInviteCodeStatus() > 0) {
                ExchangePowerActivity.startExchangePowerActivity(mContext, x, getA(), getCn(), getUrl(), getCA())
            } else {
                InviteCodeDialog(mContext, View.OnClickListener {
                    BindPoolActivity.startBindPoolActivity(mContext)
                }).show()
            }

        } else {
            showShortToast("余额不足")
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onResume() {
        super.onResume()
        BalanceTask2(cBalance, getUrl(), getA(), getCA()).execute()
    }

    private fun getA(): String {
        return intent.getStringExtra("a")
    }


    private fun getCA(): String {
        return intent.getStringExtra("ca")
    }

    private fun getUrl(): String {
        return intent.getStringExtra("url")
    }


    override fun createPresenter(): BasePresenter<IBaseView>? {
        return null
    }

    override fun createView(): IBaseView? {
        return null
    }
}